package Aleehot100;

import java.sql.Array;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * ClassName: c014
 * Package: Aleehot100
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/5/27 08:51
 * @Version 1.0
 * @Since 1.0
 */
public class c014 {
    public static void main(String[] args) {

    }

    public static int[][] merge(int[][] intervals) {
        if(intervals.length == 0) {
            return new int[][]{};
        }

        // 排序
        Arrays.sort(intervals, (int[] arr1, int[] arr2) -> {
            return arr1[0] - arr2[0];
        });

        ArrayList<int[]> list = new ArrayList<>();

        // 统计结果
        int L, R;
        for (int i = 0; i < intervals.length; i++) {
            L = intervals[i][0]; R = intervals[i][1];
            if(list.size() == 0 || list.get(list.size() - 1)[1] < L) {
                list.add(intervals[i]);
            } else {
                list.get(list.size() - 1)[1] = Math.max(list.get(list.size() - 1)[1], R);
            }
        }

        // 返回结果
        int[][] res = new int[list.size()][2];
        for (int i = 0; i < list.size(); i++) {
            res[i] = list.get(i);
        }
        return res;
    }
}
